﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Final
{
    public partial class FunctionForm : Form
    {
        public FunctionForm()
        {
            InitializeComponent();
        }


        //功能界面加载
        private void FunctionForm_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var dt = DBhelp.GetDataTable(sql);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }

        //功能界面查询按钮
        private void button1_Click(object sender, EventArgs e)
        {
            var title = Title.Text;
            var author = Author.Text;
            var sql1 = string.Format("select * from Articles where Title like '%{0}%' and Author like '%{1}%'", title, author);
            var r = DBhelp.GetDataTable(sql1);
            if (r.Rows.Count>0)
            {
                if (title != "" && author == "")
                {

                    var sql = string.Format("select * from Articles where Title like '%{0}%'", title);
                    var dt = DBhelp.GetDataTable(sql);
                    dataGridView1.DataSource = dt;
                }
                else if (title == "" && author != "")
                {
                    var sql = string.Format("select * from Articles where Title like '%{0}%'", author);
                    var dt = DBhelp.GetDataTable(sql);
                    dataGridView1.DataSource = dt;
                }
                else if (title != "" && author != "")
                {
                    var sql = string.Format("select * from Articles where Title like '%{0}%'", title);
                    var dt = DBhelp.GetDataTable(sql);
                    dataGridView1.DataSource = dt;
                }
                else if (title == "" && author == "")
                {
                    var sql = "select * from Articles";
                    var dt = DBhelp.GetDataTable(sql);
                    dataGridView1.DataSource = dt;
                }

            }
            else
            {
                MessageBox.Show("没有找到相关文章");
            }
            


        }
        
        //功能界面添加按钮
        private void button2_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            AddForm addForm = new AddForm();
            addForm.ShowDialog();
            this.Visible = true;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        //功能界面返回按钮
        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        //功能界面删除按钮
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count>0)
            {

                var r = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var d = MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (d.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", r);
                    var dt = DBhelp.UpdateDeleteAdd(sql);
                    dataGridView1.DataSource = dt;
                    MessageBox.Show("删除成功！");
                }
            }
            else
            {
                MessageBox.Show("没有选中任何行");
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
            var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
            var createdTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value.ToString();
            UpdateForm updateForm = new UpdateForm(id, title, author, createdTime);
            updateForm.Show();
        }
    }
}
